package com.hc.daoimp;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hc.dao.TenantDao;
import com.hc.entity.Tenant;
import com.hc.util.JDBCUtil;

public class TenantDaoImpl implements TenantDao {

	@Override
	public void add(Tenant tenant) {
		JDBCUtil.sql="INSERT INTO tenant VALUES(NULL,?,?,?,?,?,?,?)";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, tenant.getTname());
			JDBCUtil.ps.setInt(2, tenant.getTphone());
			JDBCUtil.ps.setString(3, tenant.getTgender());
			JDBCUtil.ps.setString(4, tenant.getTaddress());
			JDBCUtil.ps.setString(5, tenant.getTnumber());
			JDBCUtil.ps.setString(6, tenant.getTaddtime());
			JDBCUtil.ps.setInt(7, tenant.getShow());
			JDBCUtil.ps.executeUpdate();
			System.out.println("更新完成");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public List<Tenant> select(String name, int phone, String gender, String number) {
		ArrayList<Tenant> arrayList = new ArrayList<>();
		JDBCUtil.sql="select * from tenant WHERE tname like ?"
				+ " AND tphone LIKE ? AND tgender LIKE ?"
				+ "AND tnumber LIKE ? AND tshow=1";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, "%"+name+"%");
			JDBCUtil.ps.setString(2, "%"+phone+"%");
			JDBCUtil.ps.setString(3, "%"+gender+"%");
			JDBCUtil.ps.setString(4, "%"+number+"%");
			
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while(JDBCUtil.rs.next()){
				Tenant tenant = null;
				int tid = JDBCUtil.rs.getInt(1);
				String tname = JDBCUtil.rs.getString(2);
				int tphone = JDBCUtil.rs.getInt(3);
				String tgender = JDBCUtil.rs.getString(4);
				String taddress = JDBCUtil.rs.getString(5);	
				String tnumber = JDBCUtil.rs.getString(6);
				String taddtime = JDBCUtil.rs.getString(7);
				int tshow = JDBCUtil.rs.getInt(8);
				tenant=new Tenant(tid, tname, tphone, tgender, taddress, tnumber, taddtime, tshow);
				
				arrayList.add(tenant);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}
	//修改回显
	@Override
	public Tenant show(int id) {
		Tenant tenant=null;
		JDBCUtil.sql="select * from tenant WHERE tid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.rs=JDBCUtil.ps.executeQuery();
			while (JDBCUtil.rs.next()) {
				int tid = JDBCUtil.rs.getInt(1);
				String tname = JDBCUtil.rs.getString(2);
				int tphone = JDBCUtil.rs.getInt(3);
				String tgender = JDBCUtil.rs.getString(4);
				String taddress = JDBCUtil.rs.getString(5);	
				String tnumber = JDBCUtil.rs.getString(6);
				String taddtime = JDBCUtil.rs.getString(7);
				int tshow = JDBCUtil.rs.getInt(8);
				tenant=new Tenant(tid, tname, tphone, tgender, taddress, tnumber, taddtime, tshow);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tenant;
	}
	//修改

	@Override
	public void update(Tenant tenant) {
		JDBCUtil.sql="UPDATE  tenant SET tname=?, tphone=?, tgender=?,tnumber=? WHERE tid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setString(1, tenant.getTname());
			JDBCUtil.ps.setInt(2, tenant.getTphone());
			JDBCUtil.ps.setString(3, tenant.getTgender());
			JDBCUtil.ps.setString(4, tenant.getTnumber());
			JDBCUtil.ps.setInt(5, tenant.getTid());
			
			
			JDBCUtil.ps.executeUpdate();
			System.out.println("更新成功");
			JDBCUtil.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void del(int id) {
		JDBCUtil.sql="UPDATE tenant SET tshow=0 WHERE tid=?";
		JDBCUtil.ps=JDBCUtil.getPs();
		try {
			JDBCUtil.ps.setInt(1, id);
			JDBCUtil.ps.executeUpdate();
			System.out.println("跟新完成");
			JDBCUtil.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	

}
